package org.demo.algorithm.leetcode.linked;

import org.demo.algorithm.common.ListNode;

/**
 * 83
 */
public class OrderedDistinct {

    public ListNode orderedDistinct(ListNode head) {
        ListNode current = head;
        while (current != null) {
            if (current.next != null) {
                if (current.val == current.next.val){
                    current.next = current.next.next;
                    current = current.next;
                }
            }
            current = current.next;
        }
        return head;
    }

    public static void main(String[] args) {
        OrderedDistinct orderedDistinct = new OrderedDistinct();
        ListNode list = orderedDistinct.orderedDistinct(getList());
        while (list != null) {
            System.out.println(list.val);
            list = list.next;
        }
    }

    public static ListNode getList() {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(1);
        ListNode l3 = new ListNode(2);
        l1.next = l2;
        l2.next = l3;
        return l1;
    }

}
